<%@page contentType="text/html;charset=utf-8" pageEncoding="UTF-8" %>
<%@page import="org.ipo.mm.web.util.ConstantManager, java.util.Iterator,java.util.List,java.util.Collection, org.ipo.mm.ejb.entities.City,org.ipo.mm.web.util.security.SecurityManager" %>
<%@taglib prefix="h" uri="http://struts.apache.org/tags-html"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="l" uri="http://struts.apache.org/tags-logic" %>
<%@ taglib prefix="b" uri="http://struts.apache.org/tags-bean" %>

<%--
    prenosim naziv i vrednost parametra 'method' da bi se moglo prikazati submit dugme koje se gubi pri vaidaciji :)

--%>

<h1><b:message key="inst_create"/></h1>
<div id="institutionForm">
<fieldset>
<h:form styleId="addInstitutionForm"  action="/protected/institution?&method=${param['method']}">

        <table border="0">  
            <tr>
                <td><span class="important"><b:message key="inst_name"/></span></td>
                <td><h:text styleClass="required" property="name" size="100" styleId="instName" maxlength="192"/><span class="error" ><h:errors property="name" /></span></td>
            </tr>
            <tr>
                <td><span class="important"><b:message key="inst_type"/></span></td>
                <td>
                    <h:select styleClass="required" property="type">
                        <h:option value=""><b:message key="inst_choose_type"/></h:option>
                        <l:iterate id="type" name="dispatch.showCreatInstForm.types">
                            <h:option value="${type.id}">
                                <b:write name="type" property="name"/>
                            </h:option>
                        </l:iterate>

                    </h:select> <h:submit property="operation" onclick="$('#addInstitutionForm').validate().cancelSubmit = true;" styleClass="hideInDialog"><b:message key="types_create"/></h:submit>
                    <span class="error" ><h:errors property="type" /></span>
                </td>
            </tr>
            <tr>
                <td><span class="important"><b:message key="inst_family"/></span></td>
                <td>
                    <h:select styleClass="required" property="family">
                        <h:option value=""><b:message key="inst_choose_family"/></h:option>
                        <l:iterate id="family" name="dispatch.showCreatInstForm.families">
                            <h:option value="${family.id}">
                                <b:write name="family" property="name"/>
                            </h:option>
                        </l:iterate>

                    </h:select> <h:submit property="operation" onclick="$('#addInstitutionForm').validate().cancelSubmit = true;" styleClass="hideInDialog"><b:message key="families_create"/></h:submit>
                    <span class="error" ><h:errors property="family" /></span>
                </td>
            </tr>
			    <tr>
                <td><span class="important"><b:message key="inst_city"/></span></td>
                <td>
                    <h:select styleClass="required" styleId="city" property="city">
                        <h:option value=""><b:message key="inst_choose_city"/></h:option>
                        <l:iterate id="city" name="dispatch.showCreatInstForm.cities">
                            <h:option styleClass="${city.region.id}_${city.zipCode}" value="${city.id}">
                                <b:write name="city" property="name"/>
                            </h:option>
                        </l:iterate>

                    </h:select> <h:submit property="operation" onclick="$('#addInstitutionForm').validate().cancelSubmit = true;" styleClass="hideInDialog"><b:message key="cities_create"/></h:submit>
                    <span class="error" ><h:errors property="city" /></span>
                </td>
            </tr>
            <tr>
                <td><span class="important"><b:message key="inst_region"/></span></td>
                <td>
                    <h:select styleClass="required" styleId="region" property="region">
                        <h:option value="">
                            <b:message key="inst_choose_region"/>
                        </h:option>
                        <l:iterate id="region" name="dispatch.showCreatInstForm.regions">
                            <h:option value="${region.id}">
                                <b:write name="region" property="name"/>
                            </h:option>
                        </l:iterate>
                    </h:select>
                    <h:submit property="operation" onclick="$('#addInstitutionForm').validate().cancelSubmit = true;" styleClass="hideInDialog"><b:message key="regions_create"/></h:submit>
                    <span class="error" ><h:errors property="region" /></span>
                </td>
            </tr>
        
			
            <tr>
                <td><b:message key="inst_address"/></td>
                <td><h:text styleId="region" property="address" size="100" maxlength="512"/></td>
            </tr>
            <tr>
                <td><b:message key="inst_zip"/></td>
                <td><h:text property="zip"  styleId="zip" maxlength="512"/></td>
            </tr>
            <tr>
                <td><b:message key="inst_email"/></td>
                <td><h:text property="email" size="100" maxlength="512"/>
                    <span class="error" ><h:errors property="email" /></span>
                </td>
            </tr>
            <tr>
                <td><b:message key="inst_phone"/></td>
                <td><h:text property="telephone" size="100" maxlength="512"/></td>
            </tr>
            <tr>
                <td><b:message key="inst_fax"/></td>
                <td><h:text property="fax" size="100" maxlength="512"/></td>
            </tr>
            <tr>
                <td><b:message key="inst_url_long"/></td>
                <td><h:text property="web" size="100" maxlength="512"/></td>
                <span class="error" ><h:errors property="web" /></span>
            </tr>
            <tr>
                <td><b:message key="gen_note"/></td>
                <td><h:textarea property="note" cols="100" rows="10"/></td>
            </tr>

        </table>

        <!-- if user has a privilage to CREATE intitutions -->
        <%
            Collection<Integer> privileges = (Collection<Integer>) request.getSession().getAttribute(ConstantManager.SESSION_USER_PRIVILEGES);
            if (privileges.contains(SecurityManager.CREATE_INSTITUTION_PRIVILEGE)) {%>
        <c:if test="${param['method'] == 'showCreateInstForm'}">
            <h:submit property="operation" styleId="defaultButton"><b:message key="gen_create"/></h:submit>
        </c:if>
        <%}%>
        <!-- if user has a privilage to EDIT -->
        <%

            if (privileges.contains(SecurityManager.EDIT_INSTITUTION_PRIVILEGE)) {%>

        <!-- if user is owner of institution entiry -->
        <c:if test="${(param['method'] == 'showUpdateInstForm')}">

                <h:submit property="operation" styleId="defaultButton"><b:message key="gen_update"/></h:submit>

            
        </c:if>


        <%}%>
		<!--<h:cancel onclick="$('#institutionDialog').dialog('close');return false;"><b:message key="gen_cancel"/></h:cancel>-->
			<input type="reset" value="<b:message key="gen_cancel"/>" onclick="cancelForm();"/>

    </h:form>
</fieldset>

	</div>

<script>
    $("#city").change(function () {
          var str = "";
		 //alert($("#city option:selected").text()); 
		   //alert($("#city option:selected").val()); 
		   //alert($("#city option:selected").attr('class')); 
		   var classList = $("#city option:selected").attr('class');
		   var list1 = classList.split('_');
		   
		   //alert(list1[0]);
		   //alert(list1[1]);
		   $("#region").val(list1[0]); 
		   $("#zip").val(list1[1]);
		//alert();
        })
        .trigger('change');

</script>
<script type="text/javascript">
function cancelForm(){
		history.go(-1);
}
</script>